<template>
  <div>
    <div class="itxst" :ref="draggableRef">
      <div class="col">
        <slot name="leftContent"></slot>
        <draggable
          v-model.lazy="dragLeftList"
          :group="group"
          :animation="animation"
          dragClass="dragClass"
          ghostClass="ghostClass"
          chosenClass="chosenClass"
          forceFallback="true"
          :filter="`.` + filter"
          @start="onStart"
          @end="onEnd"
        >
          <transition-group>
            <div
              class="item"
              :class="item.filterClass"
              v-for="item in dragLeftList"
              :key="item.id"
            >
              {{ item.name }}
            </div>
          </transition-group>
        </draggable>
      </div>
      <div class="col">
        <slot name="rightContent"></slot>
        <draggable
          v-model.lazy="dragRightList"
          :group="group"
          :animation="animation"
          dragClass="dragClass"
          ghostClass="ghostClass"
          chosenClass="chosenClass"
          @start="onStart"
          @end="onEnd"
        >
          <transition-group>
            <div
              class="item"
              :class="item.filterClass"
              v-for="item in dragRightList"
              :key="item.id"
            >
              {{ item.name }}
            </div>
          </transition-group>
        </draggable>
      </div>
    </div>
  </div>
</template>
<script>
//导入draggable组件
import draggable from "vuedraggable";
export default {
  //注册draggable组件
  components: {
    draggable,
  },
  props: {
    //自定义ref
    draggableRef: {
      type: String,
      default() {
        return "vueDraggable";
      },
    },
    //左侧数据
    dragLeftData: {
      type: Array,
      default() {
        return [];
      },
    },
    //右侧数据
    dragRightData: {
      type: Array,
      default() {
        return [];
      },
    },
    group: {
      type: [String],
      default() {
        return "";
      },
    },
    animation: {
      type: [Number],
      default() {
        return 1000;
      },
    },
    filter: {
      //禁止拖动元素的样式名称
      type: [String],
      default() {
        return "";
      },
    },
    which: {
      type: [String, Number],
      default() {
        return 1;
      },
    },
  },
  data() {
    return {
      dragLeftList:
        this.dragLeftData && this.dragLeftData.length > 0
          ? this.dragLeftData
          : [],
      dragRightList:
        this.dragRightData && this.dragRightData.length > 0
          ? this.dragRightData
          : [],
    };
  },
  methods: {
    //开始拖拽事件
    onStart() {},
    //拖拽结束事件
    onEnd() {},
  },
};
</script>
<style scoped>
/*定义要拖拽元素的样式*/
.ghostClass {
  background-color: blue !important;
}
.chosenClass {
  background-color: red !important;
  opacity: 1 !important;
}
.dragClass {
  background-color: blueviolet !important;
  opacity: 1 !important;
  box-shadow: none !important;
  outline: none !important;
  background-image: none !important;
}
.itxst {
  margin: 10px;
}
.col {
  width: 40%;
  flex: 1;
  padding: 10px;
  border: solid 1px #eee;
  border-radius: 5px;
  float: left;
}
.col + .col {
  margin-left: 10px;
}

.item {
  padding: 6px 12px;
  margin: 0px 10px 0px 10px;
  border: solid 1px #eee;
  background-color: #f1f1f1;
}
.item:hover {
  background-color: #fdfdfd;
  cursor: move;
}
.item + .item {
  border-top: none;
  margin-top: 6px;
}
</style>